package com.iuie.ns.devtools.transfer;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.stereotype.Component;

import com.iuie.ns.devtools.element.DevElement;
import com.iuie.ns.devtools.element.JavaBeanAttributeDevElement;
import com.iuie.ns.devtools.element.JavaBeanClassDevElement;
import com.iuie.ns.devtools.element.JavaBeanGetMethodDevElement;
import com.iuie.ns.devtools.element.JavaBeanSetMethodDevElement;
import com.iuie.ns.devtools.to.DevBodyTO;
import com.iuie.ns.devtools.to.DevHeaderTO;
import com.iuie.ns.devtools.to.JavaBeanDevHeaderTO;

@Component("javaBeanTransfer")
public class JavaBeanDevTransfer extends BaseDevTransfer {
	
	@Override
	public String transfer(List<? extends DevBodyTO> list, DevHeaderTO header) {
		final List<DevElement> attributeElements = new ArrayList<>(); // 属性节点
		final List<DevElement> methodElements = new ArrayList<>(); // 方法节点
		
		if (list != null && list.size() > 0) {
			Iterator<? extends DevBodyTO> it = list.iterator();
			while(it.hasNext()) {
				DevBodyTO body = it.next();
				DevElement attributeElement = new JavaBeanAttributeDevElement(body, getFieldTypeRelationMap());
				attributeElements.add(attributeElement);
			}
			Iterator<? extends DevBodyTO> methodIt = list.iterator();
			while(methodIt.hasNext()) {
				DevBodyTO body = methodIt.next();
				DevElement getMethodElement = new JavaBeanGetMethodDevElement(body, getFieldTypeRelationMap());
				DevElement setMethodElement = new JavaBeanSetMethodDevElement(body, getFieldTypeRelationMap());
				
				methodElements.add(getMethodElement);
				methodElements.add(setMethodElement);
			}
		}
		
		DevElement classDevElement = new JavaBeanClassDevElement((JavaBeanDevHeaderTO) header) {
			
			@Override
			protected List<DevElement> getChildren() {
				List<DevElement> allElements = new ArrayList<>();
				allElements.addAll(attributeElements);
				allElements.addAll(methodElements);
				return allElements;
			}
		};
		return classDevElement.build();
	}
}
